﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Skynet.BusinessEntities;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Skynet.BusinessLogic.DataAccess
{
	public class QuestionRepository : MasterDAABRepository<BusinessEntities.BaseQuestion>, IQuestionRepository
	{



		public QuestionRepository(IUnityContainer container)
			: base(container)
		{

		}
        public BusinessEntities.BaseQuestion GetDesignQuestion(Guid key)
		{
            var text =  this.DesignTextQuestionById(key);			
			return text;
		}
        public void Update(BaseQuestion item)
        {
            if (item is TextQuestion)
            {
                var target = item as TextQuestion;
                var cmd = this.SkynetLibraryDatabase.GetSqlStringCommand("update TextQuestions set Data = @Data , Category =@Category, Title=@Title where Id=@Id");
                this.SkynetLibraryDatabase.AddInParameter(cmd, "Data", System.Data.DbType.String,  target.Question);
                this.SkynetLibraryDatabase.AddInParameter(cmd, "Category", System.Data.DbType.String, target.Category);
                this.SkynetLibraryDatabase.AddInParameter(cmd, "Title", System.Data.DbType.String, target.Title);
                this.SkynetLibraryDatabase.AddInParameter(cmd, "Id", System.Data.DbType.Guid, target.Id);
                this.SkynetLibraryDatabase.ExecuteNonQuery(cmd);
            }
            
        }
		public override void Add(BusinessEntities.BaseQuestion item)
		{
			if (item is BusinessEntities.TextQuestion)
			{
				var target = item as BusinessEntities.TextQuestion;
                var cmd = this.SkynetLibraryDatabase.GetSqlStringCommand("insert into TextQuestions values(@Id,@Data,@Category,@ExamId,@Title)");
				this.SkynetLibraryDatabase.AddInParameter(cmd, "Id", System.Data.DbType.Guid, target.Id);
				this.SkynetLibraryDatabase.AddInParameter(cmd, "Data", System.Data.DbType.String, target.Question);
				this.SkynetLibraryDatabase.AddInParameter(cmd, "Category", System.Data.DbType.String, target.Category);
				this.SkynetLibraryDatabase.AddInParameter(cmd, "ExamId", System.Data.DbType.Guid, target.ExamId);
				this.SkynetLibraryDatabase.AddInParameter(cmd, "Title", System.Data.DbType.String, target.Title);
				this.SkynetLibraryDatabase.ExecuteNonQuery(cmd);
			}
		}


        private BusinessEntities.TextQuestion DesignTextQuestionById(Guid QuestionId)
        {
            BusinessEntities.TextQuestion result = null;
            var cmd = this.SkynetLibraryDatabase.GetSqlStringCommand("select * from [TextQuestions] where Id=@QuestionId");
            this.SkynetLibraryDatabase.AddInParameter(cmd, "QuestionId", System.Data.DbType.Guid, QuestionId);
            var reader = this.SkynetLibraryDatabase.ExecuteReader(cmd);
            var mapper = MapBuilder<BusinessEntities.TextQuestion>.MapNoProperties().MapByName(c => c.Id).MapByName(c => c.Category).MapByName(c => c.Title).MapByName(c => c.ExamId).Map(c => c.Question).ToColumn("Data").Build();
            if(reader.Read())
            {
                result = mapper.MapRow(reader);                
            }
            return result;
        }
		private List<BusinessEntities.TextQuestion> DesignTextQuestionByExamId(Guid ExamId)
		{
			List<BusinessEntities.TextQuestion> result = new List<TextQuestion>();
			var cmd = this.SkynetLibraryDatabase.GetSqlStringCommand("select * from [TextQuestions] where ExamId=@ExamId order by id");
			this.SkynetLibraryDatabase.AddInParameter(cmd, "ExamId", System.Data.DbType.Guid, ExamId);
			var reader = this.SkynetLibraryDatabase.ExecuteReader(cmd);
			var mapper = MapBuilder<BusinessEntities.TextQuestion>.MapNoProperties().MapByName(c=>c.Id).MapByName(c=>c.Category).MapByName(c=>c.Title).MapByName(c=>c.ExamId).Map(c=>c.Question).ToColumn("Data").Build();
			while (reader.Read())
			{
				result.Add(mapper.MapRow(reader));
			}
			return result;
		}


        public List<BaseQuestion> DesignQuestionsByExamId(Guid Id)
		{
			var result = new List<BaseQuestion>();
            var textq = this.DesignTextQuestionByExamId(Id);			
			result.AddRange(textq);			
			return result;
		}
        public void AddRuntimeQuestion(BusinessEntities.BaseQuestion Question)
		{            
			var cmd = this.SkynetLibraryDatabase.GetSqlStringCommand("insert into QuestionByCustomer values(@Id,@Score,@ExamByCustomerId,@QuestionId,@Review,@Created,@Updated,@Kind)");
			this.SkynetLibraryDatabase.AddInParameter(cmd,"Id", System.Data.DbType.Guid, Question.CustomerQuestionId);
			this.SkynetLibraryDatabase.AddInParameter(cmd,"Score", System.Data.DbType.Decimal, 0);
			this.SkynetLibraryDatabase.AddInParameter(cmd,"ExamByCustomerId", System.Data.DbType.Guid, Question.CustomerExamId);
			this.SkynetLibraryDatabase.AddInParameter(cmd,"QuestionId", System.Data.DbType.Guid, Question.Id);
			this.SkynetLibraryDatabase.AddInParameter(cmd,"Review", System.Data.DbType.Boolean, false);
			this.SkynetLibraryDatabase.AddInParameter(cmd,"Created", System.Data.DbType.DateTime, DateTime.Now);
			this.SkynetLibraryDatabase.AddInParameter(cmd,"Updated", System.Data.DbType.DateTime, DateTime.Now);
			if ( Question is TextQuestion)
				this.SkynetLibraryDatabase.AddInParameter(cmd, "Kind", System.Data.DbType.String, "TQ");
			this.SkynetLibraryDatabase.ExecuteNonQuery(cmd);	        
		}

        public List<BaseQuestion> RuntimeByCustomerExamId(Guid ExamCustomerId)
		{
			List<BaseQuestion> Result = new List<BaseQuestion>();
			var cmdQuestions = this.SkynetLibraryDatabase.GetSqlStringCommand("select Id, QuestionId, Review, Kind from QuestionByCustomer where ExamByCustomerId=@ExamByCustomerId");
			this.SkynetLibraryDatabase.AddInParameter(cmdQuestions, "ExamByCustomerId", System.Data.DbType.Guid, ExamCustomerId);
			var qreader = this.SkynetLibraryDatabase.ExecuteReader(cmdQuestions);
			while (qreader.Read())
			{
				var kind =(string)qreader["Kind"];
				switch (kind)
				{
					case "TQ":
						var bquestion = new TextQuestion();
						bquestion.CustomerExamId = ExamCustomerId;
						bquestion.CustomerQuestionId =(Guid) qreader["Id"];
						bquestion.Id =  (Guid)qreader["QuestionId"];
						bquestion.Review = (bool)qreader["Review"];

                        var bqcommand = this.SkynetLibraryDatabase.GetSqlStringCommand("select Category,Title,Data,ExamId from TextQuestions where Id=@QuestionId");
                        this.SkynetLibraryDatabase.AddInParameter(bqcommand, "QuestionId", System.Data.DbType.Guid, bquestion.Id);
						var bqreader = this.SkynetLibraryDatabase.ExecuteReader(bqcommand);
						if (bqreader.Read())
						{
							bquestion.Category = (string)bqreader["Category"];
							bquestion.Title = (string)bqreader["Title"];
							bquestion.Question = (string)bqreader["Data"];
                            bquestion.ExamId = (Guid)bqreader["ExamId"];
						}
						Result.Add(bquestion);
						break;
					default:
						break;
				}             
			}
			return Result; 
		}
        public void LoadRuntimeQuestion(ref BusinessEntities.BaseQuestion Question)
		{
			if (Question is TextQuestion)
			{
                var target = Question as TextQuestion;
                var boolQ = this.SkynetLibraryDatabase.GetSqlStringCommand("select T.data , Q.Review from TextQuestions T join QuestionByCustomer Q on T.Id = Q.QuestionId where Q.Id=@Id ");
				this.SkynetLibraryDatabase.AddInParameter(boolQ, "Id", System.Data.DbType.Guid, Question.CustomerQuestionId);
				using (var reader = this.SkynetLibraryDatabase.ExecuteReader(boolQ))
				{
                    if (reader.Read())
                    {
                        target.Question = reader.GetString(0);
                        target.Review = reader.GetBoolean(1);
                    }
				}
			}        
		}
        public void UpdateReview(Guid CustomerQuestionId, bool review)
        {
            var cmd = this.SkynetLibraryDatabase.GetSqlStringCommand("update QuestionByCustomer set review=@review where Id=@Id");
            this.SkynetLibraryDatabase.AddInParameter(cmd, "review", System.Data.DbType.Boolean, review);
            this.SkynetLibraryDatabase.AddInParameter(cmd, "Id", System.Data.DbType.Guid, CustomerQuestionId);
            this.SkynetLibraryDatabase.ExecuteNonQuery(cmd);
        }
        public void UpdateScore(Guid CustomerQuestionId, double score)
        {
            var cmd = this.SkynetLibraryDatabase.GetSqlStringCommand("update QuestionByCustomer set score=@Score where Id=@Id");
            this.SkynetLibraryDatabase.AddInParameter(cmd, "Score", System.Data.DbType.Boolean, score);
            this.SkynetLibraryDatabase.AddInParameter(cmd, "Id", System.Data.DbType.Guid, CustomerQuestionId);
            this.SkynetLibraryDatabase.ExecuteNonQuery(cmd);
        }
	}
}